var userCode = null;
var userName = null;
var password = null;
var ruserPassword = null;
var userRole = null;
var userStatus = null;
var addBtn = null;
var backBtn = null;


$(function(){
	userCode = $("#code");
	userName = $("#name");
	password = $("#password");
	ruserPassword = $("#ruserPassword");
	userRole = $("#UserRole");
	userStatus = $("#UserStatus");
	addBtn = $("#add");
	backBtn = $("#back");
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
	userCode.next().html("*");
	userName.next().html("*");
	password.next().html("*");
	ruserPassword.next().html("*");
	userRole.next().html("*");
	userStatus.next().html("*");





	/*
	 * 验证
	 * 失焦\获焦
	 * jquery的方法传递
	 */






	ruserPassword.bind("focus",function(){
		validateTip(ruserPassword.next(),{"color":"#666666"},"* 请输入与上面一只的密码",false);
	}).bind("blur",function(){

		if(ruserPassword.val() != null && ruserPassword.val().length > 6
				&& ruserPassword.val().length < 20 && password.val() == ruserPassword.val()){
			validateTip(ruserPassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(ruserPassword.next(),{"color":"red"},imgNo + " 两次密码输入不一致，请重新输入",false);
		}
	});


	userRole.bind("focus",function(){
		validateTip(userRole.next(),{"color":"#666666"},"* 请选择用户角色",false);
	}).bind("blur",function(){
		if(userRole.val() != null && userRole.val() > 0){
			validateTip(userRole.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userRole.next(),{"color":"red"},imgNo + " 请重新选择用户角色",false);
		}
	});

	userStatus.on("focus",function(){
		console.log("1111")
		validateTip(userStatus.next(),{"color":"#666666"},"* 请选择用户状态",false);
	}).on("blur",function(){
		console.log(userStatus.val())
		if(userStatus.val() != null && userStatus.val() >= 0){
			validateTip(userStatus.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userStatus.next(),{"color":"red"},imgNo+" 请重新选择用户状态",false);
		}

	});

	addBtn.bind("click",function(){
		if(ruserPassword.attr("validateStatus") != "true"){
			ruserPassword.blur();
		}else if(userRole.attr("validateStatus") != "true"){
			userRole.blur();
		}else if(userStatus.attr("validateStatus") != "true"){
			userStatus.blur();
		} else {
			if (confirm("是否确认提交数据")) {
				$("#userForm").submit();
			}
		}

	});

	backBtn.on("click",function(){
		if(referer != undefined
			&& null != referer
			&& "" != referer
			&& "null" != referer
			&& referer.length > 4){
		 window.location.href = referer;
		}else{
			history.back(-1);
		}
	});
});